package cn.dvptech.remotemate.app

import android.app.Activity
import android.app.Application
import android.net.ConnectivityManager
import android.net.Network
import android.os.Build
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil
import cn.dvptech.remotemate.R
import cn.dvptech.remotemate.aop.Log
import cn.dvptech.remotemate.manager.ActivityManager
import cn.dvptech.remotemate.other.AppConfig
import cn.dvptech.remotemate.other.DebugLoggerTree
import cn.dvptech.remotemate.other.TitleBarStyle
import cn.dvptech.remotemate.other.ToastLogInterceptor
import cn.dvptech.remotemate.other.ToastStyle
import com.hjq.bar.TitleBar
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV
import timber.log.Timber

/**
 *    author  : ulongx
 *    website : www.dvptech.cn
 *    date    : 2024/4/24
 *    desc    : 应用入口
 */
class AppApplication : Application() {

    @Log("启动耗时")
    override fun onCreate() {
        super.onCreate()
        initSdk(this)
    }

    override fun onLowMemory() {
        super.onLowMemory()
        // 清理所有图片内存缓存
//        GlideApp.get(this).onLowMemory()
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        // 根据手机内存剩余情况清理图片内存缓存
//        GlideApp.get(this).onTrimMemory(level)
    }

    companion object {

        /**
         * 初始化一些第三方框架
         */
        fun initSdk(application: Application) {
            BGAQRCodeUtil.setDebug(true);
            // 设置标题栏初始化器
            TitleBar.setDefaultStyle(TitleBarStyle())


            // 初始化吐司
            ToastUtils.init(application, ToastStyle())
            // 设置调试模式
            ToastUtils.setDebugMode(AppConfig.isDebug())
            // 设置 Toast 拦截器
            ToastUtils.setInterceptor(ToastLogInterceptor())

            // Activity 栈管理初始化
            ActivityManager.getInstance().init(application)

            // MMKV 初始化
            MMKV.initialize(application)

            // 初始化日志打印
            if (AppConfig.isLogEnable()) {
                Timber.plant(DebugLoggerTree())
            }

            // 注册网络状态变化监听
            val connectivityManager: ConnectivityManager? = ContextCompat.getSystemService(application, ConnectivityManager::class.java)
            if (connectivityManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {
                    override fun onLost(network: Network) {
                        val topActivity: Activity? = ActivityManager.getInstance().getTopActivity()
                        if (topActivity !is LifecycleOwner) {
                            return
                        }
                        val lifecycleOwner: LifecycleOwner = topActivity
                        if (lifecycleOwner.lifecycle.currentState != Lifecycle.State.RESUMED) {
                            return
                        }
                        ToastUtils.show(R.string.common_network_error)
                    }
                })
            }
        }
    }
}